//
//  XSJEmoticonTools.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/21.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

class XSJEmoticonTools: NSObject {
    
    
    static let sharedTools : XSJEmoticonTools = XSJEmoticonTools()
    private override init() {
        super.init()
        
        
    }
    private lazy var emoticonBunle : NSBundle = {
        
        let path = NSBundle.mainBundle().pathForResource("Emoticons.bundle", ofType: nil)
        let bundle = NSBundle(path: path!)!
        
        return bundle
    }()
    
    private lazy var defaultEmoticons : [XSJEmoticon] = {
        
        return self.loadEmoticonsWithPath("default/info.plist")
        
        
    }()
    
    private lazy var emojiEmoticons: [XSJEmoticon] = {
        return self.loadEmoticonsWithPath("emoji/info.plist")
    }()
    
    //  浪小花
    private lazy var lxhEmoticons: [XSJEmoticon] = {
        return self.loadEmoticonsWithPath("lxh/info.plist")
    }()
    
    
    lazy var allEmoticons : [[[XSJEmoticon]]] = {
        
        return [
            
            self.sectionWithEmotions(self.defaultEmoticons),
            self.sectionWithEmotions(self.emojiEmoticons),
            self.sectionWithEmotions(self.lxhEmoticons)
            
        ]
        
    }()
    
    
    private func sectionWithEmotions(emoticons : [XSJEmoticon]) -> [[XSJEmoticon]] {
        let pageCount = (emoticons.count - 1) / 20 + 1
        
        var tempArray: [[XSJEmoticon]] = [[XSJEmoticon]]()
        
        
        for i in 0..<pageCount{
            
            let locaction = i * 20
            var length = 20
            
            if locaction + length > emoticons.count{
                
                length = emoticons.count - locaction
                
            }
            
            let subArray = (emoticons as NSArray).subarrayWithRange(NSMakeRange(locaction, length)) as! [XSJEmoticon]
            tempArray.append(subArray)
        }
        
        return tempArray
    }
    
    
    private func loadEmoticonsWithPath (subPath : String) -> [XSJEmoticon] {
        
        let path = self.emoticonBunle.pathForResource(subPath, ofType: nil)
        let array = NSArray(contentsOfFile: path!)
        
        var tempArray = [XSJEmoticon]()
        for dic in array! {
            
            let emoticon = XSJEmoticon(dic: dic as! [String : AnyObject])
            
            if emoticon.type! == "0" {
                
                let png = emoticon.png!
                let subPath = (path! as NSString).stringByDeletingLastPathComponent
                emoticon.path = subPath + "/" + png
            }
            
            
            tempArray.append(emoticon)
        }
        
        return tempArray
    }
    
    func emoticonWithEmoticonStr(emoticonStr :String) -> XSJEmoticon? {
        
//        for value in defaultEmoticons {
//            
//            if value.chs == emoticonStr{
//                return value
//            }
        if let defaultEmoticon = defaultEmoticons.filter({$0.chs == emoticonStr}).first {
            return defaultEmoticon

        }
//        for value in lxhEmoticons {
//            if value.chs == emoticonStr {
//                
//                return value
//                
//            }
        if let lxhEmoticon = lxhEmoticons.filter({$0.chs == emoticonStr}).first {
            return lxhEmoticon
        
            
        }
        
        
        
        
        return nil
    }
    
}
